﻿using CodeGeneratorBootstrap.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CodeGeneratorBootstrap.Util;

namespace CodeGeneratorBootstrap.Template.Service
{
    public partial class InterfaceService  : ITemplate
    {
        public MetaData Data { get; set; }
        public List<LookupClassModel> lookups { get; set; }
        public List<PickupListModel> pickuplist { get; set; }
        
        public string Filename
        {
            get { return "IService.cs"; }
        }
        public string FolderName
        {
            get { return ""; }
        }

		/// <summary>
		/// Create by: Nathawat (11-05-2014)
		/// provide method for check Class has GeoLocation attribute
		/// </summary>
		/// <param name="model">Class Model</param>
		/// <returns>bool</returns>
		public bool HasGeoLocationAttribute(ClassModel model)
		{
			foreach (AttributeModel attribute in model._attributes)
			{
				if (attribute.DataType.Name.Equals("GeoLocation"))
				{
					return true;
				}
			}
			return false;
		}

        public bool IsGenerateOneTime { get { return true; } }
        public bool IsGenerateFolder { get { return false; } }
        public bool IsRequiredOnlyOneMember { get { return false; } }
        public GenerateStyle GenerateStyle { get { return GenerateStyle.Normal; } }

        public List<ListObjectMasterDetailFilter> ListMasterDetailFilter(ClassModel cls)
        {
            ClassModel classModel = cls;

            List<AttributeModel> list = new List<AttributeModel>();
            List<ListObjectMasterDetailFilter> listMS = null;

            ClassServices.CodeGenBLClient CS = new ClassServices.CodeGenBLClient();

            list = classModel.GetAttributes().Where(a => a.DataType.Name.Equals("Master Detail")).ToList();

            if (list.Count > 0)
            {
                listMS = new List<ListObjectMasterDetailFilter>();

                foreach (var attribute in list)
                {
                    var DestClass = CS.GetClass(new ClassServices.ClassPkDto { ClassId = attribute.ChildClass.Id });
                    var filters = CS.GetMasterDetailFilter(new ClassServices.FilterPkDto { AttributeId = attribute.Id });

                    if (filters.Data != null)
                    {
                        for (int i = 0; i < filters.Data.Count; i++)
                        {
                            listMS.Add(new ListObjectMasterDetailFilter
                            {
                                Name = filters.Data[i].Name,
                                ID = filters.Data[i].Id,
                                NameID = filters.Data[i].Name + "_ID" + filters.Data[i].Id,
                                Formula = "",
                                AttributeMasterDetailName = attribute.Name,
                                ChildClassName = attribute.ChildClass.Name,
                                FilterNameModel = "Filter" + filters.Data[i].Name
                            });
                        }
                    }
                }
            }

            return listMS;
        }

        public class ListObjectMasterDetailFilter
        {
            public string Name;
            public int ID;
            public string NameID;
            public string Formula;
            public string AttributeMasterDetailName;
            public string ChildClassName;
            public string FilterNameModel;
        }

    }
}

